package com.cqs.leetcode.ds;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * Created by cqs on 2018/3/28.
 */
public class ArraysUtils {

    private static Random random = new Random();

    public static int[] randInts(int size) {
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = random.nextInt(size >> 2);
        }
        return result;
    }

    public static Integer[] randIntgers(int size) {
        Integer[] result = new Integer[size];
        for (int i = 0; i < size; i++) {
            result[i] = random.nextInt(size);
        }
        return result;
    }


    public static int[] sort(int[] nums) {
        //SortedList<Integer> list = new SortedList<Integer>(nums.length);
        if (nums == null) return nums;
        for (int i = 0; i < nums.length - 1; i++) {
            int min = nums[i];
            int index = i;
            for (int j = i; j < nums.length; j++) {
                if (nums[j] < min) {
                    min = nums[j];
                    index = j;
                }
            }
            int tmp = nums[i];
            nums[i] = nums[index];
            nums[index] = tmp;
        }
        return nums;

    }

    public static void print(int[] data) {
        System.out.println(formatInts(data));
    }


    public static List<Integer> toList(int[] nums){
        return Arrays.stream(nums).boxed().collect(Collectors.toList());
    }

    public static String formatInts(int[] data) {
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < data.length; i++) {
            builder.append(data[i] + ",");
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append("]");
        return builder.toString();
    }

    public static String formatInts(int[][] data) {
        if (data.length == 0) {
            return "[[]]";
        }
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < data.length; i++) {
            builder.append(formatInts(data[i]) + ", ");
        }
        if (builder.toString().endsWith(", ")) {
            builder.delete(builder.length() - 2, builder.length() - 1);
        }

        builder.deleteCharAt(builder.length() - 1);
        builder.append("]");
        return builder.toString();
    }

    public static String formatChars(char[] data) {
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < data.length; i++) {
            builder.append(data[i] + ",");
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append("]");
        return builder.toString();
    }

    public static String formatChars(char[][] data) {
        if (data.length == 0) {
            return "[[]]";
        }
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < data.length; i++) {
            builder.append(formatChars(data[i]) + ", ");
        }
        if (builder.toString().endsWith(", ")) {
            builder.delete(builder.length() - 2, builder.length() - 1);
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append("]");
        return builder.toString();
    }

    public static String formatBooleans(boolean[] data) {
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < data.length; i++) {
            builder.append(data[i] + ",");
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append("]");
        return builder.toString();
    }

    public boolean equationsPossible(String[] equations) {
        int[] groups = new int[26];

        for (int i = 0; i < groups.length; i++) {
            groups[i] = 'a' + i;
        }
        System.out.println(groups[0]);


        //第一遍处理==情况
        for (String s : equations) {
            char l = s.charAt(0);
            char r = s.charAt(1);
            if (s.charAt(1) == '=') {
                int val = Math.min(l, r);
                int val2 = Math.min(groups[l - 'a'], groups[r - 'a']);
                groups[l - 'a'] = Math.min(val, val2);
                groups[r - 'a'] = groups[l - 'a'];
            }
        }

        //第二遍处理不等于
        for (String s : equations) {
            char l = s.charAt(0);
            char r = s.charAt(1);
            if (s.charAt(1) == '!' && groups[l-'a'] == groups[r-'a']) {
                return false;
            }
        }
        return true;
    }

}
